<?php
// use util...
AppCore::using('Util_Config');
AppCore::using('Util_Reg');
/**
 * core class
 * usings all
 * and prepare all
 */
class AppCore
{
    // make sure no double require happens
    static protected $_usings = array();


    /**
     * using function
     * @path the path to the file
     * @once if true, only once
     * @return void
     */
    static public function using($path, $once = true)
    {
        // check if it's in holder already...

        if ($once) {
            $_key = md5($path);
            if (isset(self::$_usings[$_key])) {
                // add count...
                $tmp = explode(' ', self::$_usings[$_key]);
                $tmp[2] += 1;
                self::$_usings[$_key] = implode(' ', $tmp);
                unset($tmp);
                return;
            }
        }

        // interprete path
        $path = strtolower($path);
        if (strpos($path, '_') !== false && strpos($path,'/') === false) {
            // replace to make perfect...
            $path = str_replace('_', '/', $path);
            $path = LIBDIR . $path;
        } else {
            $path = APPDIR . $path;
        }

        $path .=  '.php';

        // otherwise... add it in.
        if ($once) {
            self::$_usings[$_key] = $path . ' x 1';
        }

        // test if exists...
        if (!file_exists($path)) {
            throw new Exception('AppCore::using(' . $path . ') - File doesn\'t exist.');
        }

        if ($once) {
            require_once $path;
        } else {
            include $path;
        }

    }// end using


    /**
     * get a singleton dao
     * @param str $dao the dao name
     * @return Dao_Base
     */
    static public function getDao($dao)
    {
        self::using($dao);
        // check reg...
        $objD = Util_Reg::get($dao);
        if ($objD instanceof $dao) {
            return $objD;
        }

        $objD = new $dao;
        Util_Reg::register($dao, $objD);
        unset($objD);
        return Util_Reg::get($dao);

    }// end getDao


    /**
     * dump the var...
     */
    static public function dump()
    {
        echo '<pre>';
        $vars = func_get_args();
        foreach ($vars as $var) {
            var_dump($var);
        }
        echo '</pre>';

    }// end dump


    // test func: what's been required?
    static public function _required()
    {
        self::dump(self::$_usings);

    }// end _included
}
?>